/*
  同步操作   直接修改State
*/
const mutations = {
  // 点击菜单
  clickMenu (state, menu) {
    // 用户点击的菜单
    let activeName = menu.name;
    let activePath = menu.url;
    // 获取state中的tabList
    let list = state.tabList;
    // 判断tabList中是否存在，存在不添加，不存在则添加
    let isExist = list.some(item => {
      // some方法就是查找方法，如果找到返回true，并且不再执行
      if (item.name == activeName) {
        return true
      }
    })
    // 返回false   表示不存在
    if (!isExist) {
      list.push({
        url: activePath,
        name: activeName,
        title: menu.name
      })
    }
    state.activePath = activePath;
    state.tabList = list;
  },
  // 点击tab标签
  clickTab (state, url) {
    state.activePath = url
  },
  //点击关闭标签
  closeTab (state, e) {
    let index = ''
    let url = ''
    let list = state.tabList; //获取动态的tab标签列表
    for (let i = 0; i < list.length; i++) {
      if (list[i].name === e.name) {
        index = i
        url = list[i].url
      }
    }
    // let index = list.findIndex(s => s.name === e.name); //拿到下标
    console.log(index);
    if (index > 0) {
      list.splice(index, 1)
      this.commit('removeKeepAliveCache', e.name);
    }
    // 当前页
    if (list.length && url === state.activePath) {
      //表示当前tab为最后一个，点击关闭后到前一个
      if (index === list.length) {
        let url = list[index - 1].url
        state.activePath = url
        e.router.push(url)
      } else {
        let url = list[index].url
        state.activePath = url
        e.router.push(url)
      }
      return
    }
  },
  //快捷关闭
  tabTools (state, type) {
    let list = state.tabList; //VueX存取的tab
    let activePath = state.activePath; //当前路
    let keepAliveList = [];
    let index = list.findIndex(item => item.url === activePath)
    if (type === 'left') {
      // 关闭左侧
      //将不清除的name保存起来供页面缓存使用
      for (let i = index; i < list.length; i++) {
        keepAliveList.push(list[i].name)
      }
      //替换页面缓存数组
      state.KeepAlive = keepAliveList
      //剔除1到index-1的数据
      list.splice(1, index - 1)
    } else if (type === 'right') {
      //关闭右侧
      //将不清除的name保存起来供页面缓存使用
      for (let i = 1; i <= index; i++) {
        keepAliveList.push(list[i].name)
      }
      //替换页面缓存数组
      state.KeepAlive = keepAliveList
      //去除index+1到最后的数据
      list.splice(index + 1)
    } else {
      //关闭其他
      let tabs = [state.tabList[0]]; //VueX存取的tab
      for (let i = 1; i < list.length; i++) {
        if (list[i].url === activePath) {
          tabs.push(list[i])
        } else {
          this.commit('removeKeepAliveCache', list[i].name);
        }
      }
      state.tabList = tabs
    }

  },
  // 关闭全部
  closeAll (state, e) {
    state.tabList = [state.tabList[0]] //改变存储的tabList
    state.KeepAlive = [] //清空缓存
    state.activePath = state.tabList[0].url //设置首页路由
    e.router.push(state.tabList[0].url) //跳转首页路由
  },
  //退出登录
  logout (state) {
    state.tabList = [state.tabList[0]] //改变存储的tabList
    state.KeepAlive = [] //清空缓存
    state.activePath = state.tabList[0].url //设置首页路由
    state.user.token = '' //token清空
    state.user.userInfo = {}  //清空用户信息

  },
  //添加页面缓存
  addKeepAliveCache (state, name) {
    //表示是首页,不进行缓存
    if (name === 'welcome') {
      return
    }
    //不是首页添加时判断是否已经存在
    if (state.KeepAlive.indexOf(name) == -1) {
      //不存在,则添加
      state.KeepAlive.push(name)
    }
  },
  // 删除页面缓存
  removeKeepAliveCache (state, name) {
    let cache = state.KeepAlive
    for (let i = 0; i < cache.length; i++) {
      if (cache[i] === name) {
        cache.splice(i, 1);
      }
    }
  },
}

export default mutations